//
//  MyDownLoadViewController.m
//  A_Comic
//
//  Created by sos on 16/1/25.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "MyDownLoadViewController.h"
#import "MoreTableViewCell.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "HomePageModel.h"
@interface MyDownLoadViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,retain) NSMutableArray *dataArr;
@property (nonatomic,retain) UIImageView *img0;

@end

@implementation MyDownLoadViewController
- (void)dealloc
{
    [_arr release];
    [_dataArr release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArr = [NSMutableArray array];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height+20) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.tableView release];
    
    [self data];
    
}
-(void)data
{
    [self.dataArr addObject:self.arr];
    
    for (NSMutableArray *arr in self.dataArr) {
        
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];
        HomePageModel *m = arr.lastObject;
        NSLog(@"%@",m.current_img_url);
                NSURL *url = [NSURL URLWithString:m.current_img_url];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
    
    
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
    NSURL *downloadURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSLog(@"沙盒路径：%@",NSHomeDirectory());
    return [downloadURL URLByAppendingPathComponent:[response suggestedFilename]];
    
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
    
    
//    UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:filePath]];
//    self.img0.image = img;
 NSLog(@"File downloaded to: %@", filePath);
    
}];
        
        [downloadTask resume];
    }
    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//    HomePageModel *m = self.dataArr[indexPath.row][indexPath.row];
//    [cell.img sd_setImageWithURL:[NSURL URLWithString:m.cover_url]];
//    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:m.cover_url]];
//    self.img0 = [[UIImageView alloc]initWithFrame:CGRectMake(50, 0, 100, 50)];
//    self.img0.backgroundColor = [UIColor yellowColor];
//    [cell addSubview:self.img0];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
